home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / freeze / makeconfig.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.2 KB  |  58 lines

  1. import regex
  2.  
  3.  
  4. # Write the config.c file
  5.  
  6. never = ['marshal', '__main__', '__builtin__', 'sys']
  7.  
  8. def makeconfig(infp, outfp, modules):
  9.     m1 = regex.compile('-- ADDMODULE MARKER 1 --')
  10.     m2 = regex.compile('-- ADDMODULE MARKER 2 --')
  11.     while 1:
  12.         line = infp.readline()
  13.         if not line: break
  14.         outfp.write(line)
  15.         if m1 and m1.search(line) >= 0:
  16.             m1 = None
  17.             for mod in modules:
  18.                 if mod in never:
  19.                     continue
  20.                 outfp.write('extern void init%s();\n' % mod)
  21.         elif m2 and m2.search(line) >= 0:
  22.             m2 = None
  23.             for mod in modules:
  24.                 if mod in never:
  25.                     continue
  26.                 outfp.write('\t{"%s", init%s},\n' %
  27.                         (mod, mod))
  28.     if m1:
  29.         sys.stderr.write('MARKER 1 never found\n')
  30.     elif m2:
  31.         sys.stderr.write('MARKER 2 never found\n')
  32.  
  33.  
  34. # Test program.
  35.  
  36. def test():
  37.     import sys
  38.     if not sys.argv[3:]:
  39.         print 'usage: python makeconfig.py config.c.in outputfile',
  40.         print 'modulename ...'
  41.         sys.exit(2)
  42.     if sys.argv[1] == '-':
  43.         infp = sys.stdin
  44.     else:
  45.         infp = open(sys.argv[1])
  46.     if sys.argv[2] == '-':
  47.         outfp = sys.stdout
  48.     else:
  49.         outfp = open(sys.argv[2], 'w')
  50.     makeconfig(infp, outfp, sys.argv[3:])
  51.     if outfp != sys.stdout:
  52.         outfp.close()
  53.     if infp != sys.stdin:
  54.         infp.close()
  55.  
  56. if __name__ == '__main__':
  57.     test()
  58.